﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Drawing.Design;

namespace OpenComposite.Workflow
{
	[ToolboxItem(typeof(HorizontalFaultHandlerActivityToolboxItem))]
	[Designer(typeof(HorizontalFaultHandlerDesigner))]
	public class HorizontalFaultHandlerActivity : HorizontalSequenceActivity, ITypeFilterProvider
	{
		internal static readonly DependencyProperty FaultProperty = DependencyProperty.Register("Fault", typeof(Exception), typeof(HorizontalFaultHandlerActivity));
		public static readonly DependencyProperty FaultTypeProperty = DependencyProperty.Register("FaultType", typeof(Type), typeof(HorizontalFaultHandlerActivity), new PropertyMetadata(DependencyPropertyOptions.Metadata));

		[ReadOnly(true), MergableProperty(false)]
		public Exception Fault
		{
			get { return ( base.GetValue(FaultProperty) as Exception ); }
		}

		[Editor(typeof(TypeBrowserEditor), typeof(UITypeEditor))]
		[MergableProperty(false)]
		public Type FaultType
		{
			get { return (Type)base.GetValue(FaultTypeProperty); }
			set { base.SetValue(FaultTypeProperty, value); }
		}

		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		{
			return base.Execute(executionContext);
		}

		#region ITypeFilterProvider Members

		public bool CanFilterType(Type type, bool throwOnError)
		{
			bool canFilter = TypeProvider.IsAssignable(typeof(Exception), type);
			return canFilter;
		}

		public string FilterDescription
		{
			get { return "Exceptions"; }
		}

		#endregion
	}


	public class HorizontalFaultHandlerDesigner : HorizontalSequenceDesigner
	{
		protected override void OnPaint(ActivityDesignerPaintEventArgs e)
		{
			base.OnPaint(e);
		}
	}

	public class HorizontalFaultHandlerActivityToolboxItem : ActivityToolboxItem
	{
		public HorizontalFaultHandlerActivityToolboxItem(Type type)
			: base(type)
		{
			this.DisplayName = "Fault Handler";
		}
	}
}
